import axios from 'axios'
// import {Message} from 'element-ui'
import domMessage from './messageOpenOnce'
import router from '../router'
const messageOnce = new domMessage()
//1. 创建新的axios实例，
const request = axios.create({
    // baseURL,
    // baseURL: "/api",
    // timeout: 3 * 1000
  })
  // 2.请求拦截器
  request.interceptors.request.use(config => {
    if(config.url == '/login'){}else{
       var  token = sessionStorage.getItem("token")

      config.headers = {
        // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', //配置请求头
        'X-Access-Token': token
      }
    }
  return config
 

}, error => {
  Promise.reject(error)
})

//响应拦截器
request.interceptors.response.use(response => {

  if(response.code==401){

  messageOnce.warning({
    message: '当前账号已过期，请重新登录！',
    type: 'warning'
  })
   sessionStorage.removeItem('token');
   sessionStorage.removeItem('rw');
   router.replace('/login').catch(error=>{error})
  }

  return response
},
error => {
  // debugger
  // Message({
  //     showClose: true,
  //     message: 'Token失效',
  //     type: 'error',
  //     duration: 2000
  // })
  if(error.response.request.status==401){

    messageOnce.warning({
      message: '当前账号已过期，请重新登录！',
      type: 'warning'
    })
     sessionStorage.removeItem('token');
     sessionStorage.removeItem('rw');
     router.replace('/login').catch(error=>{error})
    }
  




  return Promise.reject('error');


})


export default request